package com.liangzai.spring.demo.controller;

import com.liangzai.spring.demo.model.Message;
import com.liangzai.spring.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    MessageService messageService;
    //List<Message> list = new ArrayList<>();

    @PostMapping(value = "/publish",produces = "application/json")
    //@PostMapping("/publish")
    public String publish(@RequestBody Message messageInfo){
        if (!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getMessage())) {
            return "{ \"ok\":0}";
            //return false;
        }
        //存储留言
        //list.add(messageInfo);
        messageService.addmessage(messageInfo);
//        messageInfoList.add(messageInfo);
        return "{ \"ok\":1}";
        //return true;
    }

    @GetMapping("/getList")
    public List<Message> show(){
        return messageService.showmessage();
    }
}




